Q I'm writing a
background-only application
which typically has very small memory requirements. However, in some
circumstances the application needs a lot of memory for a short
period of time. I would use temporary memory, but
Inside Macintosh:
Memory says that I can't leave that memory locked around calls to
WaitNextEvent . Should I use the system heap instead?
|
A No. DTS believes that it's better for your application to allocate and lock temporary memory than to allocate memory in the system heap. This opinion is based on a number of factors, described below. |
The original reference from Inside Macintosh: Memory (pp. 2-10) is:
[...] you must never lock temporary memory across calls to
GetNextEvent
orWaitNextEvent
[...]
However, the text should read:
[...] you should avoid locking temporary memory across calls to
GetNextEvent
orWaitNextEvent
[...]
If you leave a temporary memory handle locked while yielding time to other processes, you run the risk of fragmenting the Process Manager heap. If the Process Manager heap is fragmented, the user may not be able to launch an application, even though there is enough total memory to launch it. This is a very frustrating user experience, partly because there is no way for a user to work out which application is responsible for the block that's fragmenting the heap. |
However, if your memory requirements are indeed temporary, it is acceptable to allocate the memory, lock it, use it for a while, and then unlock or deallocate it. This is certainly better than any of the alternatives: |
|
If you do lock a temporary memory handle, you should call
MoveHHi on it beforehand. This acts to minimize Process
Manager heap fragmentation. |
-- Quinn "The Eskimo!"
Worldwide Developer Technical Support
Technical Q&As
Previous Question |
Contents
To contact us, please use the Contact
Us page.